//
//  ICMSeleteItemView.m
//  ICrEAM
//
//  Created by rainimute on 16/10/27.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMSeleteItemView.h"

@interface ICMSeleteItemView()

@property (nonatomic, copy) NSString *title;

@property (nonatomic, strong) UILabel *selecteLabel;

@property (nonatomic, strong) UIButton *selecteButton;

@end

@implementation ICMSeleteItemView
#pragma mark    初始化
- (instancetype)initSeleteItemViewWithTitle:(NSString *)title isHaveInput:(BOOL)isHaveInput sectionTitle:(NSString*)sectionTitle{
    if (self = [super initWithFrame:CGRectMake(0, 0, 0, 20)]) {
        self.isHaveInput = isHaveInput;
        if (isHaveInput) {
            title = [NSString stringWithFormat:@"%@:",title];
        }
        self.title = title;
        CGSize titleSize = [title sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
        self.selecteLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
        [self.selecteLabel setUpLabelWithText:[NSString changeUinicodeStringToNSString:@"\\U2610"] alignment:NSTextAlignmentCenter textSize:16 textColor:[UIColor grayColor]];
        [self addSubview:self.selecteLabel];
        //
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(22, 0, titleSize.width, 20)];
        [titleLabel setUpLabelWithText:title alignment:NSTextAlignmentLeft textSize:16 textColor:[UIColor blackColor]];
        [self addSubview:titleLabel];
        //
        self.selecteButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(titleLabel.frame), 16)];
        [self.selecteButton addTarget:self action:@selector(selecteButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.selecteButton];
        if (isHaveInput) {
            self.inputTextField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame)+1, 0, 40, 19)];
            self.inputTextField.borderStyle = UITextBorderStyleNone;
            self.inputTextField.font = ContentFont;
            [self addSubview:self.inputTextField];
            UILabel *sepLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.inputTextField.frame)+1, 19, 60, 1)];
            sepLabel.backgroundColor = [UIColor blackColor];
            [self addSubview:sepLabel];
        }
        self.frame = CGRectMake(0, 0, CGRectGetMaxX(titleLabel.frame)+(isHaveInput?41:0), 20);
    }
    return self;
}

+ (instancetype)creatSeleteItemViewWithTitle:(NSString *)title isHaveInput:(BOOL)isHaveInput sectionTitle:(NSString*)sectionTitle{
    return [[ICMSeleteItemView alloc]initSeleteItemViewWithTitle:title isHaveInput:isHaveInput sectionTitle:sectionTitle];
}
#pragma mark   选择按钮点击
- (void)selecteButtonAction:(UIButton*)sender{
    sender.selected = !sender.selected;
    self.isSelected = sender.selected;
    NSString *selecteLabelString = sender.selected?@"\\U2611":@"\\U2610";
    self.selecteLabel.text = [NSString changeUinicodeStringToNSString:selecteLabelString];
    CGSize textSize = [self.selecteLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
    [self.selecteLabel changeWidthToOtherWidth:textSize.width];
}
#pragma mark    根据code设置按钮状态
- (void)setupSelecteButtonWithCode:(NSString *)code{
    self.selecteButton.selected = [code isEqualToString:ICMEnumIntToString(ICMEnumYESOrNOYES)];
    self.isSelected = self.selecteButton.selected;
    NSString *selecteLabelString = self.selecteButton.selected?@"\\U2611":@"\\U2610";
    self.selecteLabel.text = [NSString changeUinicodeStringToNSString:selecteLabelString];
    CGSize textSize = [self.selecteLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
    [self.selecteLabel changeWidthToOtherWidth:textSize.width];
}

- (NSString *)inputText{
    return self.inputTextField.text;
}

- (void)setInputText:(NSString *)inputText{
    self.inputTextField.text = inputText;
}

@end











